﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace FirmaTransportowa
{
    class EnumUtils
    {
        public static string getEnumDescription(Enum value)
        {
            FieldInfo fi = value.GetType().GetField(value.ToString());

            DescriptionAttribute[] attributes =
                (DescriptionAttribute[])fi.GetCustomAttributes(typeof(DescriptionAttribute), false);

            if (attributes != null && attributes.Length > 0)
                return attributes[0].Description;
            else
                return value.ToString();
        }

        public static TransportStatus getTransportStatusByDescription(string description) {
            if (description == getEnumDescription(TransportStatus.IN_PROGRESS)) {
                return TransportStatus.IN_PROGRESS;
            } else if (description == getEnumDescription(TransportStatus.PLANNED)) {
                return TransportStatus.PLANNED;
            }
            return TransportStatus.CLOSED;
        }

        public static PackageStatus getPackageStatusByDescription(string description) { 
            if (description == getEnumDescription(PackageStatus.NEW)) {
                return PackageStatus.NEW;
            } else if (description == getEnumDescription(PackageStatus.DELIVERED)) {
                return PackageStatus.DELIVERED;
            } else if (description == getEnumDescription(PackageStatus.RECEIVED)) {
                return PackageStatus.RECEIVED;
            } else if (description == getEnumDescription(PackageStatus.SORTED)) {
                return PackageStatus.SORTED;
            }
            return PackageStatus.CLOSED;
        }

        public static PackageStatus getPackageStatus(int value) {
            return (PackageStatus)System.Enum.GetValues(typeof(PackageStatus)).GetValue(value);
        }

        public static TransportStatus getTransportStatus(int value)
        {
            return (TransportStatus)System.Enum.GetValues(typeof(TransportStatus)).GetValue(value);
        }
    }
}
